﻿// ===============================================================================
// Project Name        :    ResourcesFile4DotNet
// Project Description :   
// ===============================================================================
// Class Name          :    ResourcesFile4DotNetFrom
// Class Version       :    v1.0.1.0
// Class Description   :    从资源文件中获取图片资源
// Author              :    HUANGKX 小飞兔
// Create Time         :    2018/1/19 16:20:15
// Update Time         :    2018/1/19 17:30:15
// ===============================================================================
// Copyright 2018 HUANGKX 小飞兔 . 
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ===============================================================================

using System;
using System.Collections;
using System.Drawing;
using System.IO;
using System.Resources;
using System.Windows.Forms;

namespace Resource2FileFrom
{
    public partial class frmResource2File : Form
    {
        public frmResource2File()
        {
            InitializeComponent();
        }

        private void frmResource2File_Load(object sender, EventArgs e)
        {
            //初始化加载时加载资源文件进行设置，可以视为调用的实例
        }

        private void btnSelFilePath_Click(object sender, EventArgs e)
        {
            //选择资源文件目录
            OpenFileDialog dlgOpenFile = new OpenFileDialog();
            dlgOpenFile.DefaultExt = "resources";
            dlgOpenFile.Filter = "资源文件(*.resources)|*.resources";
            if (DialogResult.OK == dlgOpenFile.ShowDialog(this))
                txtResourcesFileName.Text = dlgOpenFile.FileName;
        }

        private void btnSelImage_Click(object sender, EventArgs e)
        {
            //选择图片资源所在目录
            FolderBrowserDialog dlgImageFolder = new FolderBrowserDialog();
            dlgImageFolder.Description = "请选择图片资源保存地址";
            if (DialogResult.OK == dlgImageFolder.ShowDialog(this))
                txtImagePath.Text = dlgImageFolder.SelectedPath;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                //Resources资源文件路径
                string sResPath = txtResourcesFileName.Text;
                string sImagePath = txtImagePath.Text;

                if (!File.Exists(sResPath))
                {
                    MessageBox.Show("资源文件不存在，请重新输入", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                IResourceReader reader = new ResourceReader(sResPath);
                IDictionaryEnumerator en = reader.GetEnumerator();

                Image img;
                while (en.MoveNext())
                {
                    img = en.Value as Image;

                    string sImgFormatExtension = getImageExtension(img);

                    img.Save(sImagePath + "//" + en.Key.ToString() + sImgFormatExtension);
                }

                MessageBox.Show("导出完成", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch(Exception er)
            {
                MessageBox.Show(er.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }            
        }

        /// <summary>
        /// 根据图片的格式来确定图片后缀名
        /// </summary>
        /// <param name="img">图片</param>
        /// <returns>图片后缀名</returns>
        private string getImageExtension(Image img)
        {
            //默认png格式
            string sImgFormatExtension = ".png";

            if (img.RawFormat.Guid == System.Drawing.Imaging.ImageFormat.Bmp.Guid)
                sImgFormatExtension = ".bmp";
            else if (img.RawFormat.Guid == System.Drawing.Imaging.ImageFormat.Emf.Guid)
                sImgFormatExtension = ".emf";
            else if (img.RawFormat.Guid == System.Drawing.Imaging.ImageFormat.Exif.Guid)
                sImgFormatExtension = ".exif";
            else if (img.RawFormat.Guid == System.Drawing.Imaging.ImageFormat.Gif.Guid)
                sImgFormatExtension = ".gif";
            else if (img.RawFormat.Guid == System.Drawing.Imaging.ImageFormat.Icon.Guid)
                sImgFormatExtension = ".icon";
            else if (img.RawFormat.Guid == System.Drawing.Imaging.ImageFormat.Jpeg.Guid)
                sImgFormatExtension = ".jpeg";
            else if (img.RawFormat.Guid == System.Drawing.Imaging.ImageFormat.Png.Guid)
                sImgFormatExtension = ".png";
            else if (img.RawFormat.Guid == System.Drawing.Imaging.ImageFormat.Tiff.Guid)
                sImgFormatExtension = ".tiff";
            else if (img.RawFormat.Guid == System.Drawing.Imaging.ImageFormat.Wmf.Guid)
                sImgFormatExtension = ".wmf";

            return sImgFormatExtension;
        }

        private void btnCacnle_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
